<?php
namespace User\Controller;

use Think\Controller;

/***
 * Class WorkOrderController
 * @package User\Controller
 *   工单服务与开户下载
 *
 */
class WorkOrderController extends Controller{
    //新建工单
    public function WorkOrder_add(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        if(empty($_POST)){
            $this->assign('user',$user);
            $this->display();
        }else{
            $data['user_id'] = $user['user_id'];
            $data['title'] = I('title')?I('title'):die("标题不可为空");
            $data['type'] = I('type')?I('type'):1;
            $data['describe'] = I('content')?I('content'):die("详细说明不可为空");
            $data['email'] = I('email')?I('email'):die("E-mail不可为空");
            $data['phone'] = I('phone')?I('phone'):die("手机号不可为空");
            $data['time'] = time();

            if(!empty($_FILES['file']['size']) || !empty($_FILES['file2']['size']) || !empty($_FILES['file3']['size'])) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->autoSub = false;
                $upload->maxSize = 20145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'JPG');    // 设置附件上传类型
                $upload->rootPath = './Uploads/';                     // 设置附件上传根目录
                $upload->savePath = 'work_order/';                                  // 设置附件上传（子）目录
                $info = $upload->upload();
                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {// 上传成功 获取上传文件信息
                    $data['file'] = 'Uploads/' . $info['file']['savepath'] . $info['file']['savename'];
                    $data['file2'] = 'Uploads/' . $info['file2']['savepath'] . $info['file2']['savename'];
                    $data['file3'] = 'Uploads/' . $info['file3']['savepath'] . $info['file3']['savename'];
                }
            }
            $add_order = M('work_order')->add($data);
            if($add_order){
                $apply['k_id'] = $user['user_id'];
                $apply['order_id'] = $add_order;
                $apply['sys_state'] = 1;
                $apply['time'] = time();
                $order_apply = M('order_reply')->add($apply);
                if($order_apply){
                    echo '<script language="JavaScript">alert("提交成功");location.href="WorkOrder_add.html";</script>';
                }else{
                    M('work_order')->where(['id'=>$add_order])->delete();
                    echo '<script language="JavaScript">alert("提交失败");location.href="WorkOrder_add.html";</script>';
                }

            }else{
                echo '<script language="JavaScript">alert("提交失败");location.href="WorkOrder_add.html";</script>';
            }
        }
    }
    //撤销工单
    public function Revoke_order(){
        $id = I('id');
        if(empty($id)){$this->ajaxReturn(array('error'=>"错误的操作",'status'=>0));}
        $del = M('work_order')->where(['id'=>$id])->save(['state'=>4]);
        if($del){
            $this->ajaxReturn(array('status'=>1,'msg'=>"撤销成功"));
        }else{
            $this->ajaxReturn(array('status'=>0,'msg'=>"撤销失败"));
        }
    }
    //我的工单
    public function MyWorkOrder(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        $count =  M('work_order')->where(array('user_id'=>$user['user_id']))->count();
        $Page = new \Think\Page($count, 10);
        $order_list = M('work_order')->where(array('user_id'=>$user['user_id']))
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('id desc')
            ->select();
        $show = $Page->show();// 分页显示输出
        foreach ($order_list as $k=>$v){
            if($v['type']==1){
                $order_list[$k]['type']="账号类";
            }elseif($v['type']==2){
                $order_list[$k]['type']="资金类";
            }elseif($v['type']==3){
                $order_list[$k]['type']="操作类";
            }elseif($v['type']==4){
                $order_list[$k]['type']="申请类";
            }
            if($v['state']==1){
                $order_list[$k]['sys_state']="等待处理";
                $order_list[$k]['speed']="0%";
            }else if($v['state']==2){
                $order_list[$k]['sys_state']="处理中";
                $order_list[$k]['speed']="50%";
            }elseif($v['state']==3){
                $order_list[$k]['sys_state']="已完成";
                $order_list[$k]['speed']="100%";
            }else{
                $order_list[$k]['sys_state']="已撤销";
                $order_list[$k]['speed']="100%";
            }
            $order_list[$k]['time']=timeFormat($v['time']);//date('Y-m-d H:i:s',$v['time']);
        }

        $this->assign('page',$show);
        $this->assign('list',$order_list);
        $this->display();
    }
    //工单详情
    public function work_order_detail(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        $id =I('id')?I('id'):die("没有信息,请重试");//436 ;//

        $work = M('work_order')->where(array('id'=>$id))->find();
        $data = M('order_reply')->where(array('order_id'=>$id))->select();
        if($work['type']==1){
            $work['type']="账号类";
        }elseif ($work['type']==2){
            $work['type']="资金类";
        }elseif ($work['type']==3){
            $work['type']="操作类";
        }elseif ($work['type']==4){
            $work['type']="申请类";
        }

        if($work['state']==1){
            $work['state']="等待处理";
            $work['state_jd'] = '0%';
        }elseif($work['state']==2){
            $work['state']="处理中";
            $work['state_jd'] = '50%';
        }elseif($work['state']==3){
            $work['state']="已完成";
            $work['state_jd'] = '100%';
        }elseif($work['state']==4){
            $work['state']="已撤销";
            $work['state_jd'] = '0%';
        }
        $work['time'] = timeFormat($work['time']);//date('Y年m月d日 H:i:s',$work['time']);

        foreach ($data as $k=>$v){
            if($v['type']==1){
                $data[$k]['type']="账号类";
            }elseif($v['type']==2){
                $data[$k]['type']="资金类";
            }elseif($v['type']==3){
                $data[$k]['type']="操作类";
            }elseif($v['type']==4){
                $data[$k]['type']="申请类";
            }
            if($v['sys_state']==1){
                $data[$k]['state']="已提交";
            }elseif($v['sys_state']==2){
                $data[$k]['state']="处理中";
            }elseif($v['state']==3){
                $data[$k]['state']="订单已完成";
            }else{
                $data[$k]['state']="已撤销";
            }

            $data[$k]['time']= timeFormat($v['time']);//date('Y年m月d日 H:i:s',$v['time']);
        }

        $this->assign('user',$user);
        $this->assign('data',$data);
        $this->assign('work',$work);
        $this->display();
    }

    /**
     *  开户链接
     *
     */
    public function open_account(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页

        $this->assign('user',$user);
        $this->display();
    }


}